/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.embajada.controller;

import edu.upc.embajada.entity.Beca;
import edu.upc.embajada.entity.CriterioBusqueda;
import edu.upc.embajada.service.BecasService;
import edu.upc.embajada.ws.jms.BecasQueueService;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author u02728
 */
@Controller("becasController")
public class BecasController {

//    @Autowired
//    private BecasService becasService;
    private static Logger log = LoggerFactory.getLogger(BecasController.class);

    @RequestMapping(value = "/becas/buscar", method = RequestMethod.GET)
    public ModelAndView buscar() {
        ModelAndView mav = new ModelAndView("becas/buscar");

        CriterioBusqueda criterio = new CriterioBusqueda();
        mav.getModelMap().put("criterio", criterio);
        return mav;
    }

    @RequestMapping(value = "/becas/buscar", method = RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status, HttpSession session) {
        ModelAndView mav = new ModelAndView("becas/buscar");

        try {
            // esto era cuando se mandaba a una grilla que mostraba la información
            // List<Beca> becas = becasService.obtenerPorTipo(criterio.getIdCarrera(), criterio.getIdTipoEstudio());
            // session.setAttribute("becas", becas);
            // status.setComplete();
            //mav = new ModelAndView("redirect:/pages/becas/resultado");

            // se cambió por esta que simplemente muestre un mensage luego de grabar la cola
            BecasQueueService bService = new BecasQueueService();
            bService.grabaCola(criterio.getIdPais(), criterio.getIdCarrera(), criterio.getIdTipoEstudio(), criterio.getEmail());

            status.setComplete();
            mav = new ModelAndView("redirect:/pages/becas/resultado2");

            return mav;

        } catch (Exception ex) {
            mav.addObject("criterio", criterio);
            mav.addObject("msg", ex.getMessage());
        }
        return mav;

    }

    @RequestMapping("/becas/resultado")
    public ModelAndView resultado(HttpSession session) {

        List<Beca> becas = (List<Beca>) session.getAttribute("becas");

        ModelAndView mav = new ModelAndView("becas/resultado");
        mav.getModelMap().put("becas", becas);
        return mav;
    }

    @RequestMapping("/becas/resultado2")
    public ModelAndView resultado2(HttpSession session) {

        ModelAndView mav = new ModelAndView("becas/resultado2");
        return mav;
    }
}
